package com.zhangmeng.tools.music.qq;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Random;

/**
 * @author : 芊芊墨客
 * @version : 1.0
 * @date : 2023-04-22 09:44
 */
public class QqEncrypt {
    private static final String encNonce="CJBPACrRuNy7";
    private static final String signPrxfix="zza";
    private static final char[] dir="0234567890abcdefghijklmnopqrstuvwxyz".toCharArray();
    /**
     *
     *@param encParams 需要加密的参数，这是一段请求体数据，为json字符串格式，例如下面的格式，可以抓包获取
     *                 {"comm":{"ct":24,"cv":0},"vip":{"module":"userInfo…baseinfo_v2","param":{"vec_uin":["3011429848"]}}}
     *
     * @return 加密的方式为固定字串 zza加上一个10-16位的随机字符串再加上 固定字串 CJBPACrRuNy7加上请求数据拼接的 MD5值
     */
    public static String getSign(String encParams){
        return signPrxfix+uuidGenerate()+ Md5Encrypt.convertToMd5(encNonce+encParams);
    }

    private static String uuidGenerate(){
        int minLen=10;
        int maxLen=16;
        Random ran=new Random(System.currentTimeMillis());
        int ranLen=ran.nextInt(maxLen-minLen)+minLen;
        StringBuilder sb=new StringBuilder(ranLen);
        for (int i=0;i<ranLen;i++){
            sb.append(dir[ran.nextInt(dir.length)]);
        }
        return sb.toString();
    }

    public static class Md5Encrypt {
        //必须要重视编码问题了！折腾了好多天才发现MD5要用UTF-8形式加密
        public static String convertToMd5(String plainText) {
            byte[] secretBytes = null;
            try {
                secretBytes = MessageDigest.getInstance("md5").digest(plainText.getBytes("utf-8"));
            } catch (Exception e) {
                throw new RuntimeException("没有这个md5算法！");
            }
            String md5code = new BigInteger(1, secretBytes).toString(16);
            for (int i = 0; i < 32 - md5code.length(); i++) {
                md5code = "0" + md5code;
            }
            return md5code;
        }
    }
}





